const fs = require('fs')
const template = require('art-template');
const { resolve } = require('path');
const { rejects } = require('assert');
function httpReadFile(path, response) {
    fs.readFile(path, (error,data) => {
        if (error) {
        // response.setHeader('Content-Type',"text/plain;chartset=UTF-8")
            response.write('请求出错');
            response.end(error);
        } else {
            response.end(data)
        }

    })
}


// 模板引擎
function httpReadFileWithData(path,response,data) {
    fs.readFile(path, (erro, fileData) => {
        if (erro) {
            response.setHeader('Content-Type', "text/plain;charset=utf-8")
            response.write('数据加载错误');
            response.end('error');
        } else {
            response.end(template.render(fileData.toString(),data))
        }
    })
}



// 读文件
function readFile(path) {
    return new Promise((resolve, reject)=> {
        fs.readFile(path, function (error, data) {
            if (error) {
                reject(error)
            } else {
                resolve(data)
            }
        })
    })
}


//写数据

function writeFile(path,content) {
    return new Promise((resolve, reject) => {
        fs.writeFile(path, content, function (error, data) {
            if (error) {
                reject(error)
            } else {
                resolve(data)
            }
        })
    })
}
module.exports = {
    httpReadFile,
    httpReadFileWithData,
    readFile,
    writeFile
}